#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int trans(int k)
    {
        int res = 0;
        while (k)
        {
            res += (k % 10) * (k % 10);
            k /= 10;
        }
        return res;
    }
    bool isHappy(int n) {
        unordered_set<int> vis;
        int k = n;
        while (k != 1)
        {
            if (vis.find(k) != vis.end())
                return false;
            vis.insert(k);
            k = trans(k);
        }
        return true;
    }
};